package wrs.hcs.tc;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

import wrs.hcs.functions.*;



public class Transactions_WithAddOns {
	
	public static WebDriver driver;
	public static String pin, mainWindow="", lName;
	
	static
	Webdriver d = new Webdriver();
	Login log = new Login();
	Logout out = new Logout();
	AdminConsole console = new AdminConsole();
	AdminConsole_Tracker act = new AdminConsole_Tracker();
	Miscellaneous m = new Miscellaneous();
	Hcs_Admission adm = new Hcs_Admission();
	Hcs_NewPatient newPatient = new Hcs_NewPatient();
	AdminConsole_Single acs = new AdminConsole_Single();
	WebApp_Single s = new WebApp_Single();
	WebApp_Outpatient o = new WebApp_Outpatient();
	AdminConsole_Pair pconsole = new AdminConsole_Pair();
	
	public WebDriver startUp(String type) throws Exception{
		driver = d.startUp();
		log.admin(driver);
		console.add_reservation(driver, type);
		console.new_patient_button(driver);
		m.refresh_window(driver);
		return driver;
	}
	public String fill_up_form(WebDriver driver, String package_type, String transaction_type, boolean addon) throws Exception{
		console.input_the_reservation_type_of_patient(driver, "walkin");
		String name = console.input_the_names_of_patient(driver);
		console.input_the_birthday_of_patient(driver);
		console.input_the_gender_of_patient(driver, "male");
		console.input_the_contact_number_of_patient(driver);
		
		if(package_type.equals("online") && transaction_type.equals("inpatient")){
			
			console.input_the_package_of_patient(driver, "onlinecardio");
			console.select_unit_single(driver, "admin");
			console.input_the_room_type_of_patient(driver, "private");
			
		}else if(package_type.equals("nononline") && transaction_type.equals("outpatient")){
			
			console.input_the_package_of_patient(driver, "outpatient");
			
		}else if(package_type.equals("online") && transaction_type.equals("outpatient")){
			
			console.input_the_package_of_patient(driver, "onlineoverweight");
			console.select_unit_single(driver, "admin");
			console.input_the_room_type_of_patient(driver, "opd");
			
		}else if(package_type.equals("nononline") && transaction_type.equals("inpatient")){
			
			console.input_the_package_of_patient(driver, "outnononline");
			console.select_unit_single(driver, "admin");
			console.input_the_room_type_of_patient(driver, "private");
			
		}else if(package_type.equals("pairnononline") && transaction_type.equals("inpatient")){
			
			console.input_the_package_of_patient(driver, "pairnononline");
			console.select_unit_single(driver, "admin");
			console.input_the_room_type_of_patient(driver, "private");
			
		}
		if(addon==true){
			console.create_addon(driver, package_type, "single");
		}
		String date;
		if(!mainWindow.equals("")||!mainWindow.isEmpty()){
			date = console.input_the_date_of_admission_of_patient(driver,"");
			console.input_the_time_of_admission_of_patient(driver);
			console.input_the_booker_of_patient(driver);
			console.select_patient_care_officer(driver);
			console.input_the_responsible_for_patient(driver, "self");
			for(int y=0; y<8; y++){driver.findElement(By.xpath("//label[contains(.,'With Letter of Authorization (LOA)')]")).sendKeys(Keys.ARROW_UP);}
			console.select_doctor_single(driver, "pair", mainWindow);
		}else{
			console.select_doctor_single(driver, "single", "");
			date = console.input_the_date_of_admission_of_patient(driver,"");
			console.input_the_time_of_admission_of_patient(driver);
			console.input_the_booker_of_patient(driver);
			console.select_patient_care_officer(driver);
			console.input_the_responsible_for_patient(driver, "self");
		}
		String returned_string = name+","+date;
		return returned_string;
	}
	public void save_form(WebDriver driver) throws Exception{
		console.submit_form(driver);
		System.out.println(driver.findElement(By.xpath("//p[@id='errorMessage']")).getText());
		console.close_popup_after_save_new_patient(driver);
	}
	public String patient_2_only(WebDriver driver, String package_type, boolean addon) throws Exception{
		String name = console.input_the_names_of_patient(driver);
		console.input_the_birthday_of_patient(driver);
		if(!package_type.equals("pairnononline")){
			console.input_the_gender_of_patient(driver, "male");
		}
		console.input_the_contact_number_of_patient(driver);
		if(package_type.equals("online")){
			
			console.input_the_package_of_patient(driver, "onlinecardio");
			
		}else if(package_type.equals("nononline")){
			
			console.input_the_package_of_patient(driver, "outpatient");
			
		}
		if(addon==true){
			console.create_addon(driver, package_type, "single");
		}
		console.input_the_booker_of_patient(driver);
		console.select_patient_care_officer(driver);
		console.input_the_responsible_for_patient(driver, "self");
		for(int y=0; y<8; y++){driver.findElement(By.xpath("//label[contains(.,'With Letter of Authorization (LOA)')]")).sendKeys(Keys.ARROW_UP);}
		console.select_doctor_single(driver, "pair", mainWindow);
		return name;
	}
	
	//------------------------------------------------------------------------------------------------------------
	//THIS PORTION CORRESPONDS TO THE SCENARIOS OF ADDONS
	//LEGEND FOR COMBINATIONS:
	// i = Inpatient
	// o = Outpatient
	// On = Online Package
	// Non = NonOnline Package
	// a = Addons
	// p = pair transactions NOTE: pair transaction are only applicable for inpatient
	// swa = single from webapp with addon
	// pwa = pair from webapp with addon
	
	public void i_On_a() throws Exception{
		driver = startUp("single");
		String returned_string = fill_up_form(driver, "online", "inpatient", true);
		String[] arrString = returned_string.split(",");
		String lastName = arrString[0];
		String firstName = arrString[1];
		String admDate = arrString[2];
		save_form(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, firstName, false);
		console.table_validation(driver, "online", "inpatient");
		String text = m.set_dropdown_to_hcs("queue");
		console.select_dropdown_to_hcs(driver, text, "single");
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, "", false);
		driver.close();
	}
	public void o_Non_a() throws Exception{
		driver = startUp("single");
		String returned_string = fill_up_form(driver, "nononline", "outpatient", true);
		String[] arrString = returned_string.split(",");
		String lastName = arrString[0];
		String firstName = arrString[1];
		String admDate = arrString[2];
		save_form(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, firstName, false);
		console.table_validation(driver, "nononline", "outpatient");
		String text = m.set_dropdown_to_hcs("outpatient");
		console.select_dropdown_to_hcs(driver, text, "single");
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, "", false);
		driver.close();
	}
	public void i_Non_a() throws Exception{
		driver = startUp("single");
		String returned_string = fill_up_form(driver, "nononline", "inpatient", true);
		String[] arrString = returned_string.split(",");
		String lastName = arrString[0];
		String firstName = arrString[1];
		String admDate = arrString[2];
		save_form(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, firstName, false);
		console.table_validation(driver, "nononline", "inpatient");
		String text = m.set_dropdown_to_hcs("queue");
		console.select_dropdown_to_hcs(driver, text, "single");
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, "", false);
		driver.close();
	}
	public void o_On_a() throws Exception{
		driver = startUp("single");
		String returned_string = fill_up_form(driver, "online", "outpatient", true);
		String[] arrString = returned_string.split(",");
		String lastName = arrString[0];
		String firstName = arrString[1];
		String admDate = arrString[2];
		save_form(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, firstName, false);
		console.table_validation(driver, "online", "outpatient");
		String text = m.set_dropdown_to_hcs("outpatient");
		console.select_dropdown_to_hcs(driver, text, "single");
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName, firstName, admDate);
		console.get_status_and_click(driver, "", false);
		driver.close();
	}
	@SuppressWarnings("unused")
	public void p_On_a() throws Exception{
		driver = startUp("pair");
		mainWindow = driver.getWindowHandle();
		driver.switchTo().frame(driver.findElement(By.name("patient1")));
		String returned_string = fill_up_form(driver, "online", "inpatient", true);
		String[] arrString1 = returned_string.split(",");
		String lastName1 = arrString1[0];
		String firstName1 = arrString1[1];
		String admDate1 = arrString1[2];
		console.submit_form(driver);
		console.close_error_popup_after_submit_form(driver);
		for(int z=0; z<26; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_UP);	
		}
		driver.findElement(By.xpath("//a[@id='patientTwoTab']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.name("patient2")));
		String returned_string2 = patient_2_only(driver, "online", true);
		String[] arrString2 = returned_string2.split(",");
		String lastName2 = arrString2[0];
		String firstName2 = arrString2[1];
		save_form(driver);
		console.wellness_search_it(driver, lastName1, firstName1, admDate1);
		console.get_status_and_click(driver, firstName1, false);
		console.pair_table_validation(driver, "admin_online", "inpatient");
		String text = m.set_dropdown_to_hcs("queue");
		console.select_dropdown_to_hcs(driver, text, "pair");
		Thread.sleep(20000);
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName1, firstName1, admDate1);
		console.get_status_and_click(driver, "", false);
		driver.close();
	}
	@SuppressWarnings("unused")
	public void p_Non_a() throws Exception{
		driver = startUp("pair");
		mainWindow = driver.getWindowHandle();
		driver.switchTo().frame(driver.findElement(By.name("patient1")));
		String returned_string = fill_up_form(driver, "pairnononline", "inpatient", true);
		String[] arrString1 = returned_string.split(",");
		String lastName1 = arrString1[0];
		String firstName1 = arrString1[1];
		String admDate1 = arrString1[2];
		console.submit_form(driver);
		console.close_error_popup_after_submit_form(driver);
		for(int z=0; z<26; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_UP);	
		}
		driver.findElement(By.xpath("//a[@id='patientTwoTab']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.name("patient2")));
		String returned_string2 = patient_2_only(driver, "pairnononline", true);
		String[] arrString2 = returned_string2.split(",");
		String lastName2 = arrString2[0];
		String firstName2 = arrString2[1];
		save_form(driver);
		console.wellness_search_it(driver, lastName1, firstName1, admDate1);
		console.get_status_and_click(driver, firstName1, false);
		console.pair_table_validation(driver, "admin_nononline", "inpatient");
		String text = m.set_dropdown_to_hcs("admission");
		console.select_dropdown_to_hcs(driver, text, "pair");
		Thread.sleep(20000);
		console.display_message_to_hcs(driver);
		console.wellness_search_it(driver, lastName1, firstName1, admDate1);
		console.get_status_and_click(driver, "", false);
		driver.close();
		
	}
	public void swa_i_a() throws Exception{
		driver = d.startUp();
		log.hcs(driver, "admission");
		String getIt = newPatient.createPatient(driver, "adult");
		out.hcs(driver);
		//Split string for PIN and LASTNAME
		String[] arrString = getIt.split(",");
		lName = arrString[0];
		pin = arrString[1];
		s.wellness(driver, lName, pin, false, "basic");
		driver = d.startUp();
		log.admin(driver);
		acs.addon(driver, pin, "inpatient");
		driver.close();
	}
	public void swa_o_a() throws Exception{
		driver = d.startUp();
		log.hcs(driver, "admission");
		String getIt = newPatient.createPatient(driver, "adult");
		out.hcs(driver);
		//Split string for PIN and LASTNAME
		String[] arrString = getIt.split(",");
		lName = arrString[0];
		pin = arrString[1];
		o.wellness(driver, lName, pin, false);
		driver = d.startUp();
		log.admin(driver);
		acs.addon(driver, pin, "outpatient");
		driver.close();
	}
	public void pwa_i_a() throws Exception{
		driver = d.startUp();
		log.hcs(driver, "admission");
		String patient1 = newPatient.createPatient(driver, "adult");
		out.hcs(driver); 
		//Split string for PIN and LASTNAME - Patient1
		String[] arrString = patient1.split(",");
		String lName1 = arrString[0];
		String pin1 = arrString[1];
		log.hcs(driver, "admission");
		String patient2 = newPatient.createPatient(driver, "adult");
		out.hcs(driver); 
		//Split string for PIN and LASTNAME - Patient2
		String[] arrsString = patient2.split(",");
		String lName2 = arrsString[0];
		String pin2 = arrsString[1];
		WebApp_Pair p = new WebApp_Pair();
		p.wellness(driver, pin1, pin2, lName1, lName2, false);
		driver = d.startUp();
		log.admin(driver);
		pconsole.addon(driver, pin1);
		Thread.sleep(2000);
		driver.close();
	}
	
	
}
